<?php
/**
 * 微信菜单类
 * User: narnia
 * Date: 2017/5/19
 * Time: 11:24
 */
namespace LancerWechat;
class WechatMenu extends WechatService
{
    public $key = 'menu';


    /**
     * 获取菜单
     */
    public function getMenu()
    {
        $result = json_encode([]);
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token=' . $this->config['access_token'];
        $result = \LancerWechat\WechatFunction::curl($url);
        if (!isset($result->errcode)) {
            $result = json_encode([]);
        }
        return $result;
    }

    /**
     * 创建菜单
     * @param $buttons 数组或json
     */
    public function createMenu($buttons)
    {
        if (is_array($buttons)) {
            $buttons = json_encode($buttons, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        }
        $url = $this->getUrl([
            '{access_token}' => $this->config['global']['access_token']
        ], [$this->key, 'create']);
        $result = \LancerWechat\WechatFunction::curl($url, $buttons);
        \LancerWechat\WechatLog::write($this->config['log']['path'], $result);
        return $result;
    }
}